package com.hb.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hb.common.domain.EduChapter;
import com.hb.common.domain.EduVideo;
import com.hb.common.service.EduChapterService;
import com.hb.common.service.EduVideoService;
import com.hb.common.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/chapter")
public class ChapterController {
    @Autowired
    EduVideoService eduVideoService;
    @Autowired
    EduChapterService chapterService;
    @Autowired
    EduVideoService videoService;

    @Value("${web.file}")
    private String updagepath;


    @PostMapping("/seave")
    @Operation(summary = "添加章节")
    public ResultVo getseave(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.save(eduChapter);
        if (save){
            return ResultVo.success("",eduChapter.getId());
        }
        return ResultVo.error("",null);
    }

    @PostMapping("/upload")
    @Operation(summary = "修改章节")
    public ResultVo getUpload(@RequestBody EduChapter eduChapter){
        boolean save = chapterService.saveOrUpdate(eduChapter);
        if (save){
            return ResultVo.success("修改成功!",null);
        }
        return ResultVo.error("",null);
    }

    @GetMapping("/getChildren/{id}")
    @Operation(summary = "查询章节小节")
    public ResultVo getChildren(@PathVariable String id){
        List<EduChapter> chapters = chapterService.list()
                .stream()
                .filter(d -> d.getCourseId().equals(id))
                .toList();
        List<EduChapter> chapters1 = chapters.stream()
                .peek(p -> p.setVideos(videoService.list().stream().filter(f -> f.getChapterId().equals(p.getId())).toList()))
                .toList();
        return ResultVo.success("获取章节课时成功!",chapters1);
    }
    @GetMapping("getId/{id}")
    public ResultVo get(@PathVariable String id){
        EduChapter byId = chapterService.getById(id);
        if (byId!=null){
            return ResultVo.success("",byId);
        }else{
            return ResultVo.error("",null);
        }
    }

    @GetMapping("del/{id}")
    public ResultVo getdel(@PathVariable String id){
        LambdaQueryWrapper<EduVideo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduVideo::getChapterId,id);
        boolean b = chapterService.removeById(id);
        boolean remove = false;
        File file=new File(updagepath+"/image/");
        if (file.exists()){
            file.delete();
        }
        if (b){
            remove=videoService.remove(lambdaQueryWrapper);
        }

        if (b==true||remove==true){
            return ResultVo.success("",null);
        }else{
            return ResultVo.error("",null);
        }
    }
}
